$(function(){
    lay('#version').html('-v'+ laydate.v);
    laydate.render({
        elem: '#bg_time'
    });
    laydate.render({
        elem: '#ed_time'
    });

    var is_change=false;

    var id=$('.hide_id').val();
    var goods_id=$('.goods_id').val();
    var ac_id=$(".ac_id").val();
    var T_body=document.getElementById("sku_tbody");
    $("#sku_tbody").empty();

    var num1=$('.at_name').val().length;
    $('.changeNum').text(num1)
    $('.at_name').on('keyup',function(){
        var num=$(this).val().length;
        $('.changeNum').text(num)
    })

    var goods_head=document.getElementById("goods_head");
    ajax("post","/active/getsku",{id:goods_id,aid:ac_id},
        function success(data){
            if (data.code==200) {
                var thead_str='';
                thead_str+="<th><input type='checkbox' class='checkbox total'></th><th>SKU</th><th>图片</th><th>原价</th><th>单购价</th><th>拼团价</th><th>库存</th>";
                goods_head.innerHTML=thead_str;
                var newArr=data.data.goods_attr.tbody;
                var len=data.data.goods_attr.tbody.length;
                var cdn=data.data.cdn;
                for(var i=0;i<len;i++){
                    var str='<tr><td><input type="checkbox" '+(newArr[i].checked >0?'checked':'')+' class="inputc child_ck" attr_value="'+newArr[i].attr_value+'" attr_img="'+newArr[i].images+'" original_price="'+newArr[i].original_price+'" one_price="'+newArr[i].one_price+'" collage_price="'+newArr[i].discount_price+'" inventory="'+newArr[i].inventory+'" goods_attr_id="'+newArr[i].id+'"  activity_attr_id="'+newArr[i].attribute_value_id+'" id="'+newArr[i].id+'" ></td>'
                    str+='<td>'+newArr[i].attr_value+'</td>'
                    str+='<td ><img src="'+cdn+newArr[i].images+'" style="height: 55px;width: 55px;"/></td>'
                    str+='<td>'+newArr[i].original_price+'</td>'
                    str+='<td>'+newArr[i].one_price+'</td>'
                    str+='<td>'+newArr[i].discount_price+'</td>'
                    str+='<td>'+newArr[i].inventory+'</td></tr>'
                    var Tr=document.createElement("tr");

                    var flag2=true;
                    $("#sku_tbody input").each(function(i){
                        if($("#sku_tbody input").eq(i).prop("checked")==false){
                            flag2=false;
                        }
                        $('.total').prop('checked',flag2==true?true:false)
                    })

                    Tr.innerHTML=str;
                    T_body.appendChild(Tr)
                }
                $('.cnds').val(cdn);
                $(".total").on("click",function(){
                    var is_checked=$(this).prop('checked');
                    console.log(is_checked)
                    if(is_checked){
                        console.log("true")
                        $(".checkbox").parents(".stepT_2").find("input[type='checkbox']").prop("checked",true);
                    }else{
                        console.log("false")
                        $(".checkbox").parents(".stepT_2").find("input[type='checkbox']").prop("checked",false);
                    }
                });
                $('.child_ck').on('click',function(){
                    var flag1=true;
                    $("#sku_tbody input").each(function(i){
                        if($("#sku_tbody input").eq(i).prop("checked")==false){
                            flag1=false;
                        }
                        $('.total').prop('checked',flag1==true?true:false)
                    })
                })
            }
        }
    );
    $(".qd").on("click",function(){
        save();
    })
    $(".qrxyb").on("click",function(){
        var at_name=$(".at_name").val();
        var b_time=$('.b-time').val();
        var e_time=$('.e-time').val();
        join_num=$(".join_num").val();
        if (b_time > e_time){
            layer.msg("结束的时间需大于开始时间");
        }else if (b_time+' 00:00:00' >= e_time+' 23:59:59') {
            layer.msg("结束的时间需大于开始时间")
        }else if(join_num<2||join_num>10){
            layer.msg("请输入2~10人")
        }else if(at_name==""||b_time==""||e_time==""||join_num==""){
            layer.msg("请完善信息");
        }

        $("#tab1").removeClass("active");
        $("#tab2").addClass("active");
        $(".gofirst").show();
        $(this).hide();
    });
    $(".gofirst").on("click",function(){
        $("#tab2").removeClass("active");
        $("#tab1").addClass("active");
        $(this).hide();
        $(".qrxyb").show();
    })

    $(".edit").on("click",function(){
        $(".stepT_3").addClass("step-active").siblings().removeClass("step-active");
        $(".gd-info").hide();
        $(".scs-pt").hide();
        is_change=true;
    })
    $(".syb3").on("click",function(){
        $(".stepT_2").addClass("step-active").siblings().removeClass("step-active");    
    })
    $(".xyb2").on("click",function(){
        $(".stepT_3").addClass("step-active").siblings().removeClass("step-active");
        var is_checked=$("tbody input[type='checkbox']:checked");
        var len=is_checked.length;
        var urL=$('.cnds').val();
        var Th_tbody=document.getElementById("Th_tbody");
        $("#Th_tbody").empty();
        for(var i=0;i<len;i++){
            var attr_value=$("tbody input[type='checkbox']:checked").eq(i).attr("attr_value");
            var attr_img=$("tbody input[type='checkbox']:checked").eq(i).attr("attr_img");
            var original_price=$("tbody input[type='checkbox']:checked").eq(i).attr("original_price");
            var one_price=$("tbody input[type='checkbox']:checked").eq(i).attr("one_price");
            var discount_price=$("tbody input[type='checkbox']:checked").eq(i).attr("collage_price");
            var inventory=$("tbody input[type='checkbox']:checked").eq(i).attr("inventory");
            var goods_attr_id=$("tbody input[type='checkbox']:checked").eq(i).attr("goods_attr_id");
            var activity_attr_id=$("tbody input[type='checkbox']:checked").eq(i).attr("activity_attr_id");       
            var id=$("tbody input[type='checkbox']:checked").eq(i).attr("id");

            var str1='<td><input type="text" class="no_width deep" value="'+attr_value+'" disabled></td>'
                str1+='<td><input type="hidden" class="no_width deep" value="'+attr_img+'"><img style="height: 50px;width: 83px;" src="'+urL+attr_img+'"/></td>'
                str1+='<td><input type="text" class="no_width shallow" value="'+original_price+'"></td>'
                str1+='<td><input type="text" class="no_width shallow" value="'+one_price+'" placeholder="请输入单购价"></td>'
                str1+='<td><input type="text" class="no_width shallow" value="'+discount_price+'"></td>'
                str1+='<td><input type="text" class="no_width shallow" value="'+inventory+'"></td>'
                str1+='<td style="visibility:hidden;"><input type="hidden" value="'+goods_attr_id+'"></td>'
                str1+='<td style="visibility:hidden;"><input type="hidden" value="'+activity_attr_id+'"></td>'
                str1+='<td style="visibility:hidden;"><input type="hidden" value="'+id+'"></td>'

            var Tr=document.createElement("tr");
            Tr.innerHTML=str1;
            Th_tbody.appendChild(Tr)
        }
    })

    function save(){
        $(".gd-info").addClass("step-active").siblings().removeClass("step-active");
        $(".scs-pt").css("background","#87bb33");
        var Qrinfo=document.getElementById("qrinfo");
        $("#qrinfo").empty();
        $(".scs-pt").show();
        var urL=$('.cnds').val();
        is_change=false;
        $("#Th_tbody tr").each(function(i){
            var Tr=document.createElement("tr");
            var str2="";
            var gd_value=[];
            $("#Th_tbody tr").eq(i).find("td").each(function(j){
                gd_value.push($("#Th_tbody tr").eq(i).find("input").eq(j).val());
            });
            str2+='<td><input disabled type="text" value="'+gd_value[0]+'" disabled="disabled"></td>'
            str2+='<td><input disabled type="hidden" value="'+gd_value[1]+'" disabled="disabled"><img src="'+urL+gd_value[1]+'" style="height: 55px;width: 55px;"/></td>'
            str2+='<td><input disabled type="text" value="'+gd_value[2]+'" disabled="disabled"></td>'
            str2+='<td><input disabled type="text" value="'+gd_value[3]+'" disabled="disabled"></td>'
            str2+='<td><input disabled type="text" value="'+gd_value[4]+'" disabled="disabled"></td>'
            str2+='<td><input disabled type="text" value="'+gd_value[5]+'" disabled="disabled"></td>';
            str2+='<td>0</td>';
            str2+='<td style="visibility:hidden;"><input type="hidden" value="'+gd_value[6]+'"></td>'
            str2+='<td style="visibility:hidden;"><input type="hidden" value="'+gd_value[7]+'"></td>'
            str2+='<td style="visibility:hidden;"><input type="hidden" value="'+gd_value[8]+'"></td>'

            Tr.innerHTML=str2;
            Qrinfo.appendChild(Tr);
        });
        function jungleF(){
            var flag=true;
            $("#Th_tbody tr").each(function(m){
                $("#Th_tbody tr").eq(m).find("td").each(function(n){
                    var jungleI=$("#Th_tbody tr").eq(m).find("input").eq(n).val();
                    if(jungleI==""){
                        return flag=false;
                    }
                })
            })
        }
        console.log(jungleF())
    }
    function submit(){
        var id1=$(".hide_id").val();
        var id2=$(".ac_id").val();
        var len=$("#qrinfo tr").length;
        var tArray = new Array();
        var at_name=$(".at_name").val();
        var b_time=$(".b-time").val();
        var e_time=$(".e-time").val();
        var join_num=$(".join_num").val();
        var goods_attr_id=$(".gd_id").val();
        for(var i=0;i<len;i++){
            var gd_info=[];
            for(var j=0;j<9;j++){
                gd_info.push($("#qrinfo tr").eq(i).find("input").eq(j).val()); 
            }

            tArray.push({attr_img:gd_info[1],original_price:gd_info[2],one_price:gd_info[3],collage_price:gd_info[4],inventory:gd_info[5],goods_attr_id:gd_info[6],activity_attr_id:gd_info[7],id:gd_info[8]});
        };
        if(at_name==""||b_time==""||e_time==""||join_num==""){
            layer.msg("请完善信息");
        }else if(at_name.length>45){
            layer.msg("名称不能超过45个字")
        }else if(join_num<2||join_num>10){
            layer.msg("请输入2~10人")
        } else if (b_time >= e_time) {
            layer.msg("结束的时间需大于开始时间")
        }else{
            ajax("post","/active/update",{active_attr:tArray,goods_id:id1,id:id2,active_name:at_name,active_num:join_num,begin_time:b_time,end_time:e_time},
                function success(data){
                    if(data.code==200){
                        layer.msg(data.msg);
                        setTimeout(function(){
                            location.href="/active/index";
                        }, 1000);
                    }else{
                        layer.msg(data.msg);
                    }
                }
            )
        }
    }

    $(".change").on("click",function(){
        if(!is_change){
            submit();
        }else{
            layer.confirm('还有修改尚未保存，是否保存？', {
                btn: ['保存','取消'] //按钮
            }, function(){
                save();
                submit();
            }, function(){
                
            });
        }
    })
})